package cw.itsource.producers;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import cw.itsource.utils.ConnectionUtil;

/**
 * 测试流程：
 * 1.先启动你的rabbitmq的服务
 * 2.执行下面的示例
 */
public class TopicProducer {
    private static final String EXCHANGE = "hello-exchange";

    public static void main(String[] args) throws Exception {
        //1. 创建连接
        Connection connection = ConnectionUtil.getConnection();
        //2. 获取通道
        Channel channel = connection.createChannel();
        //3. 绑定交换机，并指定订阅模式
        channel.exchangeDeclare(EXCHANGE, "fanout");
        //4. 发送消息
        for(int i = 0 ; i < 10 ; i++){
            // 消息内容
            String message = i+"Hello everyone";
            // 发布消息到Exchange
            channel.basicPublish(EXCHANGE, "", null, message.getBytes());

        }

        //5. 消息发送完后，关闭通道 ，关闭连接
        channel.close();
        connection.close();
    }
}
